#ifndef SERVER_MASTER_H_
#define SERVER_MASTER_H_
#include "server_conf.h"
#include "msg.h"


C_CODE_BEGIN

struct slave;
struct list;
struct map;
struct msgqueue;
struct master_cfg_file;
struct master_sys_log;
struct master {
	// kv object which holds the configuration script
	struct master_cfg_file* cfg_cmd;
	/* This global message queue store the pointer of the slave */
	struct msgqueue* glb_msgqueue;
	struct list* thpool;
	struct map* slavemap;
	/* This new are is added to allow configurable message dispatching chain *
	 * If one message cannot find out its destination locally where should this
	 * message go ? */
	struct list* msgchain;
	/* command for worker thread */
	const char thread_cmd[12];
	/* command map for perform extensible configuration of the master */
	struct map * cmdmap;
	/* master system log file handler */
	struct master_sys_log* syslog;
};

EXPORT_FUNC struct master* master_create( const char* cfg );
EXPORT_FUNC void master_destroy( struct master* );
EXPORT_FUNC void master_run( struct master* );
EXPORT_FUNC void master_write_log( struct master* , const char* , const char* , ... );
/* 
 * After calling the shutdown , all the master related resource can not be used
 * the master has been internally destroyed and all the thread has been signal to
 * exit properly .
 * Once you need to reboot the system , you should acquire a new master 
 */
EXPORT_FUNC void master_join( struct master* );
EXPORT_FUNC void master_execute_config( struct master* m , const char* cfg );

struct slave* _master_find_slave( struct master* m , const char* slavename );




/*
 * This structure is indeed internally used by the master module instead of other
 * external module. The specific detail structure of this data struct is for declaration
 * of the master internal command, see more detail in master_internal_cmd.h file.
 */

struct master_command {
	/* the specific command executor for the master */
	int (*cmd)( struct master* m , struct kv_element* val);
};

C_CODE_END

#endif //SERVER_MASTER_H_